//
//  YQInformationCtl.m
//  OverseasImmigration
//
//  Created by yuqin on 2019/5/20.
//  Copyright © 2019年 yuqin. All rights reserved.
//

#import "YQInformationCtl.h"
#import "YQInformationCell.h"
#import "YQInformationModel.h"
#import "YQInformationDetailVC.h"

@interface YQInformationCtl ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,DZNEmptyDataSetDelegate,DZNEmptyDataSetSource>

@property (nonatomic, strong) UICollectionView *informationCV;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation YQInformationCtl

- (NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"资讯";
    self.view.backgroundColor = WhiteColor;
    [self loadInformationData];
    [self setupInformationView];
}

- (void)loadInformationData {
    NSString *urlStr = [NSString stringWithFormat:@"%@news/newsList",BaseHttpUrl];
    NSDictionary *dict = @{@"page":@"1",
                           @"limit":@"10",
                           @"type":@"1"
                           };
    [[HTTPRequstManger sharedHTTPRequstManger] getRequestWithUrlString:urlStr appendParameters:dict successBlock:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 0) {
            //成功
            NSMutableArray *list = responseObject[@"data"][@"dataList"];
            for (NSDictionary *dict in list) {
                YQInformationModel *model = [YQInformationModel mj_objectWithKeyValues:dict];
                [self.dataArr addObject:model];
            }
            [self.informationCV reloadData];
        }
    } failureBlock:^(NSError *error) {
        
    }];
}

- (void)setupInformationView {
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat cellWidth = (SCREENWIDTH-40-32)/2.0;
    self.layout.minimumLineSpacing = 10;
    //最小item间距（默认为10）
    self.layout.minimumInteritemSpacing = 10;
    self.layout.itemSize = CGSizeMake(cellWidth, cellWidth+10);
    self.informationCV = [[UICollectionView alloc] initWithFrame:CGRectMake(16, 20, SCREENWIDTH-32, SCREENHEIGHT-20) collectionViewLayout:self.layout];
    YQ_EmptyView(self.informationCV);
    self.informationCV.backgroundColor = WhiteColor;
    self.informationCV.showsVerticalScrollIndicator = NO;
    self.informationCV.delegate = self;
    self.informationCV.dataSource = self;
    [self.informationCV registerNib:[UINib nibWithNibName:@"YQInformationCell" bundle:nil] forCellWithReuseIdentifier:@"YQInformationCell"];
    [self.view addSubview:self.informationCV];
}

YQ_Empty_Deletage_DataSources(@"暂无数据");

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    YQInformationCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YQInformationCell" forIndexPath:indexPath];
    YQInformationModel *model = self.dataArr[indexPath.item];
    cell.model = model;
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(10, 10);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
    YQInformationDetailVC *vc = [[YQInformationDetailVC alloc] init];
    YQ_PUSH(vc);
//    YQ_PUSH([])
}



@end
